package com.kucai.yifaqapp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.dagger.component.activity.DaggerAllActivityComponent;
import com.kucai.yifaqapp.dagger.module.activity.AllActivityModule;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.network.entity.res.ResActivity;
import com.kucai.yifaqapp.model.network.entity.res.ResActivitys;
import com.kucai.yifaqapp.presenter.activity.AllActivityPresenter;
import com.kucai.yifaqapp.ui.view.AllActivityView;
import com.kucai.yifaqapp.util.OnItemClickListener;
import com.kucai.yifaqapp.util.SYSOUtils;
import com.kucai.yifaqapp.widget.AlertDialogIos1;

import java.util.ArrayList;

import static com.kucai.yifaqapp.global.App.getContext;

public class AllActivity extends BaseActivity<AllActivityPresenter> implements AllActivityView {
    private ArrayList<ResActivity.DataBeanX.ActivityBean.DataBean> slidedata = new ArrayList<>();//活动

    TextView titleLeftTv;
    TextView headerTitle;
    RecyclerView allactivity;
    private ActivtyAdapter adapter;
    LinearLayout skinbg;
    private AlertDialogIos1 myDialog1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleLeftTv = findViewById(R.id.title_left_tv);
        headerTitle = findViewById(R.id.headerTitle);
        allactivity = findViewById(R.id.allactivity);
        skinbg = findViewById(R.id.skinbg);
        headerTitle.setText("全部活动");

        titleLeftTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //获取活动信息
        mPresenter.getactivity();
//        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                mPresenter.getnow(Info.user.token);
//            }
//        });

        allactivity.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ActivtyAdapter();

        allactivity.setAdapter(adapter);

    }

    @Override
    protected int setContentView() {
        return R.layout.activity_all;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:

                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }

    @Override
    protected void initDagger() {
        DaggerAllActivityComponent.builder()
                .allActivityModule(new AllActivityModule(this))
                .build().inject(this);
    }

    //获取聊天室是否存在
    @Override
    public void getActivity(ResActivity resActivity) {
        //活动
        if (resActivity.data.activity.data != null) {
            slidedata.clear();
            slidedata.addAll(resActivity.data.activity.data);
            allactivity.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getActivitys(ResActivitys resActivitys) {
        if (resActivitys.data.type.equals("fuwenben")) {
            Intent intent = new Intent(getContext(), WebActivity.class);
            Bundle bundle = new Bundle();
            SYSOUtils.sOut_(resActivitys.data);
            bundle.putString("content", resActivitys.data.doc);
            bundle.putString("title", resActivitys.data.title);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    }

//    @Override
//    public void getguanfang(ResActivitys resActivitys) {
//        Intent intent = new Intent(getContext(), WebActivity.class);
//        Bundle bundle = new Bundle();
//        SYSOUtils.sOut_(resActivitys.data);
//        bundle.putString("content", resActivitys.data.doc);
//        bundle.putString("title", resActivitys.data.title);
//        intent.putExtras(bundle);
//        startActivity(intent);
//
//    }

    /////////////////////////////////////////////////////////////////////////////////////////////////
    class ActivtyAdapter extends RecyclerView.Adapter<ActivtyAdapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.activty_item, parent, false));

        }

        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {
            if (null == slidedata.get(position).title) {
                holder.activity_tv.setVisibility(View.GONE);
            } else {
                holder.activity_tv.setVisibility(View.VISIBLE);
                holder.activity_tv.setText(slidedata.get(position).title + "");

            }
            Glide.with(getContext())
                    .load(slidedata.get(position).img)
                    .into(holder.activity_iv);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != Info.user) {
                        if (null == Info.user.userName) {
                            Toast.makeText(getContext(), "请注册为正式账号", Toast.LENGTH_SHORT).show();
                        }
                        if (slidedata.get(position).type.equals("fuwenben")) {
                            mPresenter.getactivitys(slidedata.get(position).doc, slidedata.get(position).imgid);
                        } else if (slidedata.get(position).type.equals("guanfang")) {
                            Intent intent = new Intent(getContext(), ActivityDetails.class);
                            intent.putExtra("id", slidedata.get(position).id + "");
                            intent.putExtra("imgid", slidedata.get(position).doc);
                            intent.putExtra("activitytype", slidedata.get(position).id + "");
                            if (Info.user != null) {
                                startActivity(intent);
                            }

                        } else if (slidedata.get(position).type.equals("tanchuang")) {

                            myDialog1 = new AlertDialogIos1(AllActivity.this).builder();
                            myDialog1.setCancelable(false);
                            myDialog1.setGone().setTitle(slidedata.get(position).title + "").setMsg(slidedata.get(position).content + "").
                                    setNegativeButton("取消", R.color.skyblue, new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            myDialog1.dismiss();
                                        }
                                    }).setPositiveButton("确定", R.color.skyblue, new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    myDialog1.dismiss();
                                }
                            }).show();
                        }
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return slidedata.size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView activity_tv;
            ImageView activity_iv;

            MViewHolder(View itemView) {
                super(itemView);
                activity_tv = (TextView) itemView.findViewById(R.id.activity_tv);
                activity_iv = (ImageView) itemView.findViewById(R.id.activity_iv);

            }
        }
    }
}
